package com.bbg.amyam.web.mvc.controller.views.publicview;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbg.amyam.domain.enumtype.ActiveStatusCode;
import com.bbg.amyam.domain.model.School;
import com.bbg.amyam.web.mvc.controller.dto.SchoolDTO;
import com.bbg.amyam.web.mvc.controller.response.JsonResponse;

@RequestMapping(value="/{id:[0-9a-z]{2,20}}/public_view/search/**")
@Controller
public class PublicViewSearchController {

	 @RequestMapping(produces = "text/html")
	    public String list(Model uiModel) {
	       
	            uiModel.addAttribute("schools", School.findAllSchools());
	        
	        return "public_view/search/index";
	    }
	    
	    @RequestMapping(value="/list", produces="application/json")
	  	public @ResponseBody List<SchoolDTO> list(
	  			@RequestParam(value="shortMame_startsWith", required=false) String shortname,
	      		@RequestParam(value="_zipcode", required=false) String zipcode,
	      		@RequestParam(value="filters", required=false) String filters,
	      		@RequestParam(value="page", required=false) Integer page,
	      		@RequestParam(value="rows", required=false) Integer rows,
	      		@RequestParam(value="sidx", required=false) String sidx,
	      		@RequestParam(value="sord", required=false) String sord, HttpServletRequest request) {
	  		
	        String contextPath = request.getContextPath();
	        
	  		List<School> schools = new java.util.ArrayList<School>();
			
			schools = School.findSchoolsByShortNameLikeAndActiveStatus(StringUtils.isEmpty(shortname)?"*":shortname, ActiveStatusCode.Active).getResultList();
	  				
	  		List<SchoolDTO> schoolDtos=SchoolDTO.mapping(schools,contextPath);
	  		JsonResponse<SchoolDTO> response = new JsonResponse<SchoolDTO>();
	  		response.setGeonames(schoolDtos);
	  		
	  		response.setTotalResultsCount(Long.valueOf(schoolDtos.size()).toString());
	  		
	  		return schoolDtos;
	  	}
	    
}
